﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace Microsoft.Office.PowerPoint.STAMP.Core
{
    public static class Extensions
    {
        /// <summary>
        /// Returns a formatted string value from a timespan
        /// </summary>
        /// <param name="position">Integer value to format to timespan string</param>
        /// <param name="format">Format to apply</param>
        /// <returns>Formatted string</returns>
        public static string ToTimeSpanString(this int position, string format)
        {
            TimeSpanFormatter formatter = new TimeSpanFormatter();
            var timeSpanString = formatter.Format(format, new TimeSpan(0, 0, 0, 0, position), CultureInfo.CurrentCulture);
            return timeSpanString;
        }

        /// <summary>
        /// Parses the enum types
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="strType">Type of the STR.</param>
        /// <param name="ignoreCase">if set to <c>true</c> [ignore case].</param>
        /// <param name="result">The result.</param>
        /// <returns></returns>
        public static bool EnumTryParse<T>(this string strType, bool ignoreCase, out T result)
        {
            try
            {
                result = (T)Enum.Parse(typeof(T), strType, ignoreCase);
                return true;
            }
            catch
            {
                result = default(T);
                return false;
            }
        }

        /// <summary>
        /// Trims the words.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="lengthLimit">The length limit.</param>
        /// <returns></returns>
        public static string TrimWords(this string text, int lengthLimit)
        {
            string trimmedWord = text.Substring(0, lengthLimit - 10).Trim();

            return trimmedWord.Substring(0, trimmedWord.LastIndexOf(" "))+ "...";
        }
    }
}
